package com.ruoyi.project.business.salaryoptions.service.impl;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.salaryoptions.entity.SalaryOptions;
import com.ruoyi.project.business.salaryoptions.mapper.SalaryOptionsMapper;
import com.ruoyi.project.business.salaryoptions.request.AddSalaryOptionsRequest;
import com.ruoyi.project.business.salaryoptions.request.ListSalaryOptionsRequest;
import com.ruoyi.project.business.salaryoptions.response.SalaryOptionsResponse;
import com.ruoyi.project.business.salaryoptions.service.ISalaryOptionsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 薪资计算项Service业务层处理
 * 
 * @author ${author}
 * @date 2024-02-21
 */
@Service
public class SalaryOptionsServiceImpl implements ISalaryOptionsService {
    @Autowired
    private SalaryOptionsMapper salaryOptionsMapper;

    /**
     * 查询薪资计算项
     * 
     * @return 薪资计算项
     */
    @Override
    public SalaryOptions selectSalaryOptionsById(Long id) {
        return salaryOptionsMapper.selectSalaryOptionsById(id);
    }

    /**
     * 查询薪资计算项列表
     * 
     * @return 薪资计算项
     */
    @Override
    public List<SalaryOptionsResponse> selectSalaryOptionsList(ListSalaryOptionsRequest request, Long tenantId) {
        // TODO request to  param
        SalaryOptions param = BeanUtils.copy(request, SalaryOptions.class);
        param.setTenantId(tenantId);
        List<SalaryOptions> entitis = salaryOptionsMapper.selectSalaryOptionsList(param);
        List<SalaryOptionsResponse> resList = BeanUtils.copyList(entitis, SalaryOptionsResponse.class);
        return resList;
    }

    @Override
    public List<SalaryOptions> selectSalaryOptionsByIds(List<Long> ids, Long tenantId) {
        List<SalaryOptions> entitis = salaryOptionsMapper.selectSalaryOptionsByIds(ids, tenantId);
        return entitis;
    }

    /**
     * 新增薪资计算项
     * 
     * @return 结果
     */
    @Override
    public int insertSalaryOptions(AddSalaryOptionsRequest request, Long operatorId, Long tenantId) {
        if(StringUtils.isEmpty(request.getCode())) {
            throw new CustomException("请设计编码");
        }
        checkCode(request.getCode(), tenantId);
        SalaryOptions entity = BeanUtils.copy(request, SalaryOptions.class);
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setCreator(operatorId);
        entity.setDeleted(Boolean.FALSE);
        entity.setCreateTime(DateUtils.getNowDate());
        return salaryOptionsMapper.insertSalaryOptions(entity);
    }

    /**
     * 修改薪资计算项
     * 
     * @return 结果
     */
    @Override
    public int updateSalaryOptions(SalaryOptions salaryOptions, Long operatorId, Long tenantId) {
        salaryOptions.setUpdateTime(DateUtils.getNowDate());
        salaryOptions.setTenantId(tenantId);
        salaryOptions.setUpdater(operatorId);
        return salaryOptionsMapper.updateSalaryOptions(salaryOptions);
    }

    /**
     * 批量删除薪资计算项
     * 
     * @return 结果
     */
    @Override
    public int deleteSalaryOptionsByIds(Long[] ids, Long operatorId, Long tenantId) {
        return salaryOptionsMapper.deleteSalaryOptionsByIds(ids);
    }

    /**
     * 删除薪资计算项信息
     * 
     * @return 结果
     */
    @Override
    public int deleteSalaryOptionsById(Long id, Long operatorId, Long tenantId) {
        return salaryOptionsMapper.deleteSalaryOptionsById(id);
    }

    private void checkCode(String code, Long tenantId) {
        SalaryOptions entity = salaryOptionsMapper.findByCode(code, tenantId);
        if(entity != null) {
            throw new CustomException("已存在的编码, 请更换");
        }
    }
}
